<!-- src/views/login/Login.vue -->
<template>
  <div class="login-container">
    <el-card class="login-card" shadow="hover">
      <h2 class="login-title">欢迎登录 Nebula Admin</h2>
      <el-form :model="form" :rules="rules" ref="formRef" label-width="80px">
        <!-- 账号字段 -->
        <el-form-item label="账号" prop="username">
          <el-input v-model="form.username" placeholder="请输入账号" />
        </el-form-item>

        <!-- 密码字段 -->
        <el-form-item label="密码" prop="password">
          <el-input
            v-model="form.password"
            placeholder="请输入密码"
            show-password
            type="password"
          />
        </el-form-item>

        <!-- 记住我 checkbox -->
        <el-form-item>
          <el-checkbox v-model="form.remember">记住我</el-checkbox>
        </el-form-item>

        <!-- 登录按钮 -->
        <el-form-item>
          <el-button type="primary" @click="handleLogin" :loading="loading">
            登录
          </el-button>
        </el-form-item>
        <!-- 注册账号链接 -->
        <div class="register-link">
          没有账号？
          <span class="link" @click="goRegister">注册账号</span>
        </div>
      </el-form>
    </el-card>
  </div>
</template>

<script setup>
// 引入所需模块
import { ref } from "vue";
import { ElMessage } from "element-plus";
import { useUserStore } from "@/stores/user"; // 用户 Pinia store
import { login } from "@/api/user"; // 登录 API 接口
import router from "@/router";

// 跳转到注册页面
const goRegister = () => {
  router.push("/register");
};

// 表单绑定数据
const form = ref({
  username: "",
  password: "",
  remember: true,
});

// 表单验证规则
const rules = {
  username: [{ required: true, message: "请输入账号", trigger: "blur" }],
  password: [{ required: true, message: "请输入密码", trigger: "blur" }],
};

const formRef = ref(null);
const loading = ref(false);

const userStore = useUserStore();

// 登录操作
const handleLogin = () => {
  formRef.value.validate(async (valid) => {
    if (!valid) return;

    loading.value = true;
    try {
      const res = await login(form.value);
      if (res.code === 0) {
        userStore.setToken(res.data.token);
        userStore.setUserInfo(res.data.userInfo);
        localStorage.setItem("accessToken", res.data.accessToken);
        localStorage.setItem("refreshToken", res.data.refreshToken);

        // ✅ 跳转首页
        router.push("/dashboard");
        ElMessage.success("登录成功");
      } else {
        ElMessage.error(res.message || "登录失败");
      }
    } catch (e) {
      ElMessage.error("登录出错");
    } finally {
      loading.value = false;
    }
  });
};
</script>

<style scoped>
.login-container {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
  background: url("@/assets/bg.png") no-repeat center center;
  background-size: cover; /* 拉伸填充 */
}

.login-card {
  width: 400px;
  padding: 30px;
}

.login-title {
  text-align: center;
  margin-bottom: 20px;
}

.register-link {
  text-align: right;
  margin-top: 10px;
  font-size: 14px;
}

.register-link .link {
  color: #409eff; /* Element-Plus 默认蓝色 */
  cursor: pointer;
}
.register-link .link:hover {
  text-decoration: underline;
}
</style>
